package com.data.app.commons.exception;

import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author Zyl
 * @Date 2021/3/30 15:06
 * @Version 1.0
 * @Describe: 自定义异常基类
 */
public class BaseServiceException extends RuntimeException {
    /**
     * 异常编码
     */
    private int code;

    /**
     * 异常编码
     */
    private String message;

    /**
     * 业务id
     */
    private String businessId;
    /**
     * 可设置异常附加信息
     */
    @Setter
    private String addtionalMessage;

    public <T extends Enum<T> & ErrEnumInterface> BaseServiceException(T errorEnum) {
        this(errorEnum, "");
    }

    public <T extends Enum<T> & ErrEnumInterface> BaseServiceException(T errorEnum, String message) {
        this(errorEnum, message, null);
    }

    public <T extends Enum<T> & ErrEnumInterface> BaseServiceException(T errorEnum, String message, Throwable e) {
        this(errorEnum, message, e, "");
    }

    public <T extends Enum<T> & ErrEnumInterface> BaseServiceException(T errorEnum, String message, Throwable cause, String businessId) {
        super(message, cause);
        if(StringUtils.isEmpty(message)){
            this.message = errorEnum.getMessage();
        }else{
            this.message = message;
        }
        this.code = errorEnum.getCode();
        this.businessId = businessId;
    }

    /**
     * 非枚举类中定义得异常信息构造器
     *
     * @param message
     * @param errorCode
     */
    public BaseServiceException(String message, int errorCode) {
        super(message);
        this.code = errorCode;
    }

    /*public BaseServiceException(String message, ServiceErrEnumInfo serviceErrEnumInfo) {
        //构造器的第二个参数是枚举，之后把枚举里面定义的code给了这个code
        super(message);
        this.code = serviceErrEnumInfo.getCode();
    }*/


    public int getCode() {
        return code;
    }

    public String getBusinessId() {
        return businessId;
    }

    @Override
    public String getMessage() {
        return super.getMessage();
    }
}
